#include "exti.h"

void Rst_Check_Config(void)
{
    EXTI_InitTypeDef EXTI_Initstruct;
    NVIC_InitTypeDef NVIC_Initstruct;
    GPIO_InitTypeDef GPIO_Initstruct;
    /* 	Enable AF Clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE);
    /* EXITLine Source Configration */
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Initstruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_Initstruct);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource0);
    /* NVIC configration */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_Initstruct.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_Initstruct.NVIC_IRQChannelPreemptionPriority = 0x02;
    NVIC_Initstruct.NVIC_IRQChannelSubPriority = 0x00;
    NVIC_Initstruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_Initstruct);
    /* EXTILine Configration */
    EXTI_Initstruct.EXTI_Line = EXTI_Line0;
    EXTI_Initstruct.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_Initstruct.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_Initstruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_Initstruct);

}

void RST_Check(void)
{
    State_Value[7] |= (0xff00 & ((u16)RST_Counter << 8));
}


void CLK_Check_Config(void)
{
    EXTI_InitTypeDef EXTI_Initstruct;
    NVIC_InitTypeDef NVIC_Initstruct;
    GPIO_InitTypeDef GPIO_Initstruct;
    /* 	Enable AF Clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE);
    /* EXITLine Source Configration */
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_1;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Initstruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_Initstruct);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource1);
    /* NVIC configration */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_Initstruct.NVIC_IRQChannel = EXTI1_IRQn;
    NVIC_Initstruct.NVIC_IRQChannelPreemptionPriority = 0x02;
    NVIC_Initstruct.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_Initstruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_Initstruct);
    /* EXTILine Configration */
    EXTI_Initstruct.EXTI_Line = EXTI_Line1;
    EXTI_Initstruct.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_Initstruct.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_Initstruct.EXTI_LineCmd = DISABLE;  //Disable the EXTILine
    EXTI_Init(&EXTI_Initstruct);

}

void CLK_Check(void)
{
    EXTI_InitTypeDef EXTI_Initstruct;

    /* Enable EXTI1 */
    EXTI_Initstruct.EXTI_Line = EXTI_Line1;
    EXTI_Initstruct.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_Initstruct.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_Initstruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_Initstruct);
    Delay_us(10);

    /* Disable EXTI1 */
    EXTI_Initstruct.EXTI_LineCmd = DISABLE;
    EXTI_Init(&EXTI_Initstruct);
    if(CLK_Counter > 5)
        State_Value[7] |= (0x00ff & 0x0001);
    else
        State_Value[7] |= (0x00ff & 0x0000);
    CLK_Counter = 0;
}

